﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WooCoo.ObjectModel.Validation;

namespace WooCoo.ObjectModel
{
    public abstract partial class Entity : IDataErrorInfo
    {
        private ValidationRules _validationRules;
        [IgnoreDataMember]
        public ValidationRules ValidationRules
        {
            get
            {
                if (_validationRules == null)
                {
                    _validationRules = new ValidationRules(this);
                }
                //target不做序列化，需要重设
                if (this._validationRules.Target == null)
                    this._validationRules.SetTarget(this);
                return _validationRules;
            }
        }

        #region IDataErrorInfo
        string IDataErrorInfo.Error
        {

            get
            {
                if (!IsSelfValid)
                    return this.ValidationRules.BrokenRules.ToString();
                else
                    return String.Empty;
            }
        }

        string IDataErrorInfo.this[string columnName]
        {
            get
            {
                if (!this.ValidationRules.Validate(columnName))
                {
                    return this.ValidationRules.GetErrorMessage(columnName);
                }
                return null;
            }
        }
        #endregion
    }
}
